﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class Categories :_Categories
    {
        public static Categories GetAllActiveCategories()
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                c.Where.BranchID.Value = Branches.CurrentBranchID;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetAllActiveCategories(int branchId)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                c.Where.BranchID.Value = branchId;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetCategoryByStockKey(int branchID, int stockKey)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                c.Where.BranchID.Value = branchID;
                c.Where.StockKey.Value = stockKey;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetCategoryByStockKey", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetAllActiveCategories(string ids)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                c.Where.BranchID.Value = Branches.CurrentBranchID;
                ids = ids.Trim();
                if (ids != "")
                {
                    c.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                    c.Where.ID.Value = ids;
                }
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetAllActiveCategoriesByParent(int parentID)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                c.Where.ParentID.Value = parentID;
                c.Where.BranchID.Value = Branches.CurrentBranchID;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetCategoryByName(string name, int branchID)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                c.Where.CategoryName.Value = name;
                c.Where.BranchID.Value = branchID;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetCategoryByName", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetAllWebActiveCategoriesByParent(int? parentID)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                if(parentID != null)
                    c.Where.ParentID.Value = parentID;
                else
                    c.Where.ParentID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNull;
                c.Where.BranchID.Value = Branches.CurrentBranchID;
                c.Where.VisibleToCustomer.Value = true;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }

        public static Categories GetAllWebActiveCategoriesByParent(int? parentID, bool isDealerOnly)
        {
            try
            {
                Categories c = new Categories();
                c.Where.Deleted.Value = false;
                if(parentID != null)
                    c.Where.ParentID.Value = parentID;
                else
                    c.Where.ParentID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNull;
                c.Where.BranchID.Value = Branches.CurrentBranchID;
                c.Where.IsDealerOnly.Value = isDealerOnly;
                c.Where.VisibleToCustomer.Value = true;
                c.Query.Load();

                return c;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }

        public void AddCategoryInfo(String name, int level, int? parentID, bool deleted)
        {
            try
            {
                this.CategoryName = name;
                this.Level = level;
                this.Deleted = deleted;
                if(parentID != null)
                    this.ParentID = (int)parentID;
                this.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                this.BranchID = Branches.CurrentBranchID;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetAllActiveCategories", e.StackTrace);
                throw e;
            }
        }
    }
}
